package com.sfh.cpc.com.ui;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 功能描述: TODO
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/31
 */
public class UtilTime {


    public static String getFriendlyTime(String date) {
        if (TextUtils.isEmpty(date)) {
            return date;
        }
        try {
            Date time = DATEFORMATTHREADLOCAL.get().parse(date);
            //秒
            long second = Math.abs(System.currentTimeMillis() - time.getTime()) / 1000;

            if (second >= 60 * 60 * 24) {
                return date.substring(0,10);
            }
            if (second > 60 * 60) {
                long hour = second / 3600;
//                long miniate = (second % 3600) / 60;
                return hour + "小时前";
            }
            if (second >= 60) {
                long miniate = second / 60;
                return miniate + "分钟前";
            }
            return second + "秒前";

        } catch (ParseException e) {
            return date;
        }
    }

    static ThreadLocal<SimpleDateFormat> DATEFORMATTHREADLOCAL = new ThreadLocal<SimpleDateFormat>() {

        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
}
